package cn.tedu._08security.service;

import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

public class UserDetailsServiceImpl implements UserDetailsService {
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        /*
            1.用户: 用户访问私有资源,通过Security配置类进行过滤,跳转到登录页面;
            2.用户: 用户在登录页面输入用户名和密码,点击登录按钮,发送请求给Spring Security;
            3.Security: Spring Security接收请求,调用UserDetailsService的loadUserByUsername方法;
            4.开发者: 根据用户名username到服务端数据库种查询用户信息[密码、权限];
              4.1 查询数据库密码目的:交给Security框架进行密码校验;
              4.2 查询数据库权限目的:交给Security框架进行权限校验;
         */
        return User.withUsername(username)
                .password("{noop}123456") //数据库中查询出来的[用户表]: {noop}代表使用明文,不加密;
                .authorities("sys:private:view", "sys:private:edit") //从数据库中查询出来的该用户的权限[用户表->角色表->权限表连接查询的结果]
                .build();
    }
}
